package me.spring.cloud.common.components.validator.util;

import me.spring.cloud.common.components.util.enums.inter.RegularEnum;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @description: 正则枚举验证
 * @author: luffy
 * @create: 2018-07-20 16:23
 **/
@Slf4j
public class RegularValidatorUtil {

  public static boolean isValid(RegularEnum<String> regularEnum, String field) {
    try {
      if (StringUtils.isEmpty(field)) {
        return true;
      }
      Pattern pattern = Pattern.compile(regularEnum.getRegular());
      return pattern.matcher(field).matches();
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return false;
    }
  }
}
